//  Defines the entry point for the DLL application.
//

#include "stdafx.h"
#include "GUI.h"

#include "tinyxml.h"
#include "myVisitor.cpp"


BOOL APIENTRY DllMain( HANDLE hModule, 
                       DWORD  ul_reason_for_call, 
                       LPVOID lpReserved
					 )
{
    switch (ul_reason_for_call)
	{
		case DLL_PROCESS_ATTACH:
		case DLL_THREAD_ATTACH:
		case DLL_THREAD_DETACH:
		case DLL_PROCESS_DETACH:
			break;
    }
    return TRUE;
}




void * CXML::getDocument()
{
	return m_pDocument;
}

bool CXML::openFile(const char * pszFileName)
{
	if (!pszFileName)
		return false;
	if (pszFileName[0] == 0)
		return false;
	if (m_pDocument)
		delete m_pDocument;

	m_pDocument = new TiXmlDocument(pszFileName);
	if (m_pDocument->LoadFile())
		return true;

	return false;
}

bool CXML::openFile(void * pFile)
{
	if (!pFile)
		return false;
	if (m_pDocument)
		delete m_pDocument;
	m_pDocument = new TiXmlDocument();
	if (m_pDocument->LoadFile(pFile))
		return true;

	return false;
}

void CXML::destory()
{
	delete this;
}

bool CXML::getAttribute(const char * pszNodeName ,const char * pszNameOfNode , const char * pszAttributeName , char ** szDes)
{
	if (pszNodeName == NULL ||
		pszAttributeName == NULL ||
		*szDes == NULL)
		return false;

	myVisitor visitor;
	strcpy (visitor.szStr,pszNodeName);
	strcpy (visitor.szNamePro,pszNameOfNode);
	TiXmlNode * pNode = 0;
	if (m_pCurrentElement)
	{
		if (m_pCurrentElement->Accept(&visitor))
			pNode = const_cast <TiXmlNode*> (visitor.resSult);
		m_pCurrentElement = NULL;
	}
	else if (m_pDocument->Accept(&visitor))
		pNode = const_cast <TiXmlNode*> (visitor.resSult);
	if (pNode)
	{
		char cc[256];
		strcpy(cc,((TiXmlElement*)pNode)->Attribute(pszAttributeName)); 
		strcpy(*szDes,((TiXmlElement*)pNode)->Attribute(pszAttributeName)); 
		return true;
	}
	return false;
}

bool CXML::getAttribute(const WCHAR * pszNodeName ,const WCHAR * pszNameOfNode , const WCHAR * pszAttributeName , WCHAR ** szDes)
{
	if (pszNodeName == NULL ||
		pszAttributeName == NULL ||
		*szDes == NULL)
		return false;

	DWORD dwNum = WideCharToMultiByte(CP_OEMCP,NULL,pszNodeName,-1,NULL,0,NULL,FALSE);
	char *psText1;
	psText1 = new char[dwNum];
	WideCharToMultiByte (CP_OEMCP,NULL,pszNodeName,-1,psText1,dwNum,NULL,FALSE);

	dwNum = WideCharToMultiByte(CP_OEMCP,NULL,pszAttributeName,-1,NULL,0,NULL,FALSE);
	char *psText2;
	psText2 = new char[dwNum];
	WideCharToMultiByte (CP_OEMCP,NULL,pszAttributeName,-1,psText2,dwNum,NULL,FALSE);

	dwNum = WideCharToMultiByte(CP_OEMCP,NULL,pszNameOfNode,-1,NULL,0,NULL,FALSE);
	char *psname;
	psname = new char[dwNum];
	WideCharToMultiByte (CP_OEMCP,NULL,pszNameOfNode,-1,psname,dwNum,NULL,FALSE);

	char *psText3 = new char[256];
	bool r = getAttribute(psText1,psname,psText2,&psText3);
	MultiByteToWideChar(CP_ACP, 0, psText3, 256, *szDes, 256);



	delete[] psText1;
	delete[] psText2;
	delete[] psText3;
	delete[] psname;

	return r;
}
bool CXML::getChildrensAttribute (const WCHAR * pszFatherNodeName ,const WCHAR * pszFatherNameOfNode , int num , const WCHAR * pszAttributeName,WCHAR **pszDes)
{
	if (!pszFatherNameOfNode ||
		!pszFatherNodeName)
		return false;

	DWORD dwNum = WideCharToMultiByte(CP_OEMCP,NULL,pszFatherNodeName,-1,NULL,0,NULL,FALSE);
	char *psText1;
	psText1 = new char[dwNum];
	WideCharToMultiByte (CP_OEMCP,NULL,pszFatherNodeName,-1,psText1,dwNum,NULL,FALSE);

	dwNum = WideCharToMultiByte(CP_OEMCP,NULL,pszFatherNameOfNode,-1,NULL,0,NULL,FALSE);
	char *psText2;
	psText2 = new char[dwNum];
	WideCharToMultiByte (CP_OEMCP,NULL,pszFatherNameOfNode,-1,psText2,dwNum,NULL,FALSE);

	dwNum = WideCharToMultiByte(CP_OEMCP,NULL,pszAttributeName,-1,NULL,0,NULL,FALSE);
	char *psText4;
	psText4 = new char[dwNum];
	WideCharToMultiByte (CP_OEMCP,NULL,pszAttributeName,-1,psText4,dwNum,NULL,FALSE);

	myVisitor visitor;
	strcpy (visitor.szStr,psText1);
	strcpy (visitor.szNamePro,psText2);
	delete[] psText1;
	delete[] psText2;
	TiXmlNode * pNode = 0;
	if (m_pDocument->Accept(&visitor))
		pNode = const_cast <TiXmlNode*> (visitor.resSult);

	if (!pNode)
		return false;

	TiXmlElement * Children = 0;
	int count = 0;
	
	while (Children = (TiXmlElement*)pNode->IterateChildren(Children))
	{
		count++;
		if (count == num)
		{
			char *psText3 = new char[256];
			strcpy (psText3,Children->Attribute(psText4));
			MultiByteToWideChar(CP_ACP, 0, psText3, 256, *pszDes, 256);
			delete[] psText3;
		}
	}
	delete[] psText4;
	return true;
}
bool CXML::getAttribute (const WCHAR * pszFatherNodeName ,const WCHAR * pszFatherNameOfNode , const WCHAR * pszNodeName ,const WCHAR * pszNameOfNode , const WCHAR * pszAttributeName , WCHAR ** pszDes)
{
	if (!pszFatherNameOfNode ||
		!pszFatherNodeName)
		return false;

	DWORD dwNum = WideCharToMultiByte(CP_OEMCP,NULL,pszFatherNodeName,-1,NULL,0,NULL,FALSE);
	char *psText1;
	psText1 = new char[dwNum];
	WideCharToMultiByte (CP_OEMCP,NULL,pszFatherNodeName,-1,psText1,dwNum,NULL,FALSE);

	dwNum = WideCharToMultiByte(CP_OEMCP,NULL,pszFatherNameOfNode,-1,NULL,0,NULL,FALSE);
	char *psText2;
	psText2 = new char[dwNum];
	WideCharToMultiByte (CP_OEMCP,NULL,pszFatherNameOfNode,-1,psText2,dwNum,NULL,FALSE);

	myVisitor visitor;
	strcpy (visitor.szStr,psText1);
	strcpy (visitor.szNamePro,psText2);
	delete[] psText1;
	delete[] psText2;
	TiXmlNode * pNode = 0;
	if (m_pDocument->Accept(&visitor))
		pNode = const_cast <TiXmlNode*> (visitor.resSult);

	if (!pNode)
		return false;

	m_pCurrentElement = pNode;
	bool r =getAttribute(pszNodeName ,pszNameOfNode , pszAttributeName , pszDes);

	return r;

}
bool CXML::getChildrenNum(const WCHAR * pszNodeName ,const WCHAR * pszNameOfNode , int & num)
{
	num = 0;

	DWORD dwNum = WideCharToMultiByte(CP_OEMCP,NULL,pszNodeName,-1,NULL,0,NULL,FALSE);
	char *psText1;
	psText1 = new char[dwNum];
	WideCharToMultiByte (CP_OEMCP,NULL,pszNodeName,-1,psText1,dwNum,NULL,FALSE);

	dwNum = WideCharToMultiByte(CP_OEMCP,NULL,pszNameOfNode,-1,NULL,0,NULL,FALSE);
	char *psText2;
	psText2 = new char[dwNum];
	WideCharToMultiByte (CP_OEMCP,NULL,pszNameOfNode,-1,psText2,dwNum,NULL,FALSE);

	myVisitor visitor;
	strcpy (visitor.szStr,psText1);
	strcpy (visitor.szNamePro,psText2);
	delete[] psText1;
	delete[] psText2;
	TiXmlNode * pNode = 0;
	if (m_pDocument->Accept(&visitor))
		pNode = const_cast <TiXmlNode*> (visitor.resSult);

	if (!pNode)
		return false;
	
	TiXmlNode * ch = 0;
	while (ch = pNode->IterateChildren(ch))
		num++;

	return true;
}
bool createInstance(void ** pInstance)
{
	try
	{
		*pInstance = new CXML;
		return true;
	}
	catch(...)
	{
		
	}
	return false;
}
